<template>
  <div class="course-learn">
    <div class="learn-header">
      <div class="course-info">
        <h1>{{ course.title }}</h1>
        <p>{{ course.description }}</p>
      </div>
      <div class="progress-info">
        <el-progress 
          type="circle" 
          :percentage="course.progress || 0"
          :size="80"
        />
        <span>学习进度</span>
      </div>
    </div>

    <div class="learn-content">
      <div class="chapter-list">
        <h3>课程章节</h3>
        <div class="chapters">
          <div 
            v-for="(chapter, index) in chapters" 
            :key="chapter.id"
            class="chapter-item"
            :class="{ active: currentChapter?.id === chapter.id, completed: chapter.completed }"
            @click="selectChapter(chapter)"
          >
            <div class="chapter-number">{{ index + 1 }}</div>
            <div class="chapter-info">
              <h4>{{ chapter.title }}</h4>
              <p>{{ chapter.duration }}</p>
            </div>
            <div class="chapter-status">
              <el-icon v-if="chapter.completed" color="#67C23A"><Check /></el-icon>
              <el-icon v-else-if="chapter.id === currentChapter?.id" color="#409EFF"><VideoPlay /></el-icon>
            </div>
          </div>
        </div>
      </div>

      <div class="video-content">
        <div v-if="currentChapter" class="video-player">
          <div class="video-container">
            <div class="video-placeholder">
              <el-icon size="48"><VideoPlay /></el-icon>
              <p>视频播放器占位符</p>
              <p>{{ currentChapter.title }}</p>
            </div>
          </div>
          
          <div class="chapter-description">
            <h3>{{ currentChapter.title }}</h3>
            <p>{{ currentChapter.description }}</p>
          </div>

          <div class="chapter-actions">
            <el-button 
              type="primary" 
              :disabled="!previousChapter"
              @click="goToPrevious"
            >
              上一章
            </el-button>
            <el-button 
              type="success" 
              @click="markComplete"
            >
              完成学习
            </el-button>
            <el-button 
              type="primary" 
              :disabled="!nextChapter"
              @click="goToNext"
            >
              下一章
            </el-button>
          </div>
        </div>
        
        <div v-else class="empty-state">
          <el-icon size="48"><VideoPlay /></el-icon>
          <p>请选择一个章节开始学习</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute } from 'vue-router'

const route = useRoute()

const course = ref({
  id: 1,
  title: 'Vue3 高级开发',
  description: '深入学习Vue3新特性和最佳实践',
  progress: 75
})

const chapters = ref([
  {
    id: 1,
    title: 'Vue3 基础回顾',
    description: '回顾Vue3的核心概念和基础语法',
    duration: '15分钟',
    completed: true
  },
  {
    id: 2,
    title: 'Composition API 详解',
    description: '深入学习Composition API的使用和优势',
    duration: '25分钟',
    completed: true
  },
  {
    id: 3,
    title: '响应式系统原理',
    description: '理解Vue3响应式系统的工作原理',
    duration: '30分钟',
    completed: false
  },
  {
    id: 4,
    title: '性能优化技巧',
    description: '学习Vue3应用的性能优化方法',
    duration: '20分钟',
    completed: false
  }
])

const currentChapter = ref(null)

const previousChapter = computed(() => {
  if (!currentChapter.value) return null
  const index = chapters.value.findIndex(c => c.id === currentChapter.value.id)
  return index > 0 ? chapters.value[index - 1] : null
})

const nextChapter = computed(() => {
  if (!currentChapter.value) return null
  const index = chapters.value.findIndex(c => c.id === currentChapter.value.id)
  return index < chapters.value.length - 1 ? chapters.value[index + 1] : null
})

const selectChapter = (chapter) => {
  currentChapter.value = chapter
}

const markComplete = () => {
  if (currentChapter.value) {
    currentChapter.value.completed = true
    ElMessage.success('章节学习完成！')
  }
}

const goToPrevious = () => {
  if (previousChapter.value) {
    currentChapter.value = previousChapter.value
  }
}

const goToNext = () => {
  if (nextChapter.value) {
    currentChapter.value = nextChapter.value
  }
}

onMounted(() => {
  const courseId = route.params.id
  // 加载课程数据
  
  // 默认选择第一个未完成的章节
  const firstUncompleted = chapters.value.find(c => !c.completed)
  if (firstUncompleted) {
    currentChapter.value = firstUncompleted
  }
})
</script>

<style scoped>
.course-learn {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.learn-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.course-info h1 {
  margin: 0 0 8px 0;
  color: #2c3e50;
}

.course-info p {
  color: #7f8c8d;
  margin: 0;
}

.progress-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.progress-info span {
  color: #7f8c8d;
  font-size: 14px;
}

.learn-content {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 20px;
}

.chapter-list {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.chapter-list h3 {
  margin: 0 0 20px 0;
  color: #2c3e50;
}

.chapter-item {
  display: flex;
  align-items: center;
  padding: 15px;
  border-radius: 8px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: all 0.3s;
}

.chapter-item:hover {
  background: #f5f7fa;
}

.chapter-item.active {
  background: #f0f9ff;
  border: 1px solid #1890ff;
}

.chapter-item.completed {
  background: #f6ffed;
}

.chapter-number {
  width: 30px;
  height: 30px;
  background: #e1e1e1;
  color: #666;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  font-weight: bold;
}

.chapter-item.active .chapter-number {
  background: #1890ff;
  color: white;
}

.chapter-item.completed .chapter-number {
  background: #67C23A;
  color: white;
}

.chapter-info {
  flex: 1;
}

.chapter-info h4 {
  margin: 0 0 4px 0;
  font-size: 14px;
}

.chapter-info p {
  margin: 0;
  color: #7f8c8d;
  font-size: 12px;
}

.video-content {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.video-player {
  height: 100%;
}

.video-container {
  background: #000;
  border-radius: 8px;
  margin-bottom: 20px;
  height: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.video-placeholder {
  text-align: center;
  color: white;
}

.video-placeholder .el-icon {
  margin-bottom: 16px;
}

.chapter-description {
  margin-bottom: 20px;
}

.chapter-description h3 {
  margin: 0 0 10px 0;
  color: #2c3e50;
}

.chapter-description p {
  color: #7f8c8d;
  line-height: 1.6;
}

.chapter-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.empty-state {
  text-align: center;
  padding: 60px;
  color: #7f8c8d;
}

.empty-state .el-icon {
  margin-bottom: 16px;
}

@media (max-width: 768px) {
  .learn-content {
    grid-template-columns: 1fr;
  }
  
  .learn-header {
    flex-direction: column;
    text-align: center;
  }
  
  .progress-info {
    margin-top: 20px;
  }
}
</style>